import java.util.Arrays;

public class BubbleSortTest {
	public static void main(String[] args) {
		lanZhouTestBubbleSort();
		testSelectSort();
		testSearch();
	}

	public static void lanZhouTestBubbleSort() {
		int[] ages = { 3, 9, 7, 2, 1, 4 };
		int tempValue = ages[0];
		for (int i = 0; i < ages.length; i++) {
			for (int j = 0; j < ages.length - i - 1; j++) {
				if (ages[j] > ages[j + 1]) {
					tempValue = ages[j];
					ages[j] = ages[j + 1];
					ages[j + 1] = tempValue;
				}
			}
			System.out.println(" 每次排序后结果Arrays.toString(ages) = " + Arrays.toString(ages));
		}
		System.out.println(" 最终排序后结果Arrays.toString(ages) ----> " + Arrays.toString(ages));
	}

	public static void testSelectSort() {
		System.out.println("SortDemo.testSelectSort  -----选择排序");
		int[] ages = { 3, 9, 7, 2, 1, 4 };
		for (int i = 0; i < ages.length; i++) {
			int minIndex = i;
			// 找出最小值的下标
			for (int j = i + 1; j < ages.length; j++) {
				if (ages[j] < ages[minIndex]) {
					minIndex = j;
				}
			}

			//            如果最小值的下标比当前循环的i也要大,那么最小值和当前i做交换(注意:i是从0开始计数)
			if (minIndex > i) {
				int tempValue = ages[i];
				ages[i] = ages[minIndex];
				ages[minIndex] = tempValue;
			}
			System.out.println(" 每次排序后结果Arrays.toString(ages) = " + Arrays.toString(ages));
		}
		System.out.println(" 最终排序后结果Arrays.toString(ages) ----> " + Arrays.toString(ages));
	}
	
	public static void testSearch() {
		int[] ages = { 1, 2, 3, 4, 5, 6 };
		int low = 0;
		int heigh = ages.length;

		//  要查找的数
		int searchInt = 6;

		while (low <= heigh) {
			int mid = (low + heigh) / 2;
			if (searchInt == ages[mid]) {
				System.out.println("要找的数的下标为 = " + mid);
				break;
			} else if (searchInt > ages[mid]) {
				low = mid + 1;
			} else if (searchInt < ages[mid]) {
				heigh = mid - 1;
			}
		}
	}

}


